package com.meihua.baoleyi.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.GoodsBean;

import java.util.ArrayList;

/**
 * 首页RecycleView适配器
 */
public class MainRVAdapter extends RecyclerView.Adapter<MainRVAdapter.MViewHolder> {
    private ArrayList<GoodsBean.DataBean> mData;
    private Context context;

    public MainRVAdapter(Context context,ArrayList<GoodsBean.DataBean> mData) {
        this.mData = mData;
        this.context=context;
    }

    @Override
    public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(final MViewHolder holder, int position) {
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    class MViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_txt;
        public TextView tv_money;
        public ImageView iv_img;

        public MViewHolder(View itemView) {
            super(itemView);
            tv_txt = itemView.findViewById(R.id.tv_txt);
            tv_money = itemView.findViewById(R.id.tv_money);
            iv_img = itemView.findViewById(R.id.iv_img);
        }

        public void setData(final GoodsBean.DataBean bean) {
            tv_txt.setText(bean.getGoods_name());
            tv_money.setText("￥" + bean.getShop_price());
            Glide.with(itemView.getContext()).load(bean.getOriginal_img()).placeholder(R.mipmap.chanpin).into(iv_img);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(bean);

                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(GoodsBean.DataBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnItemClickListener mOnItemClickListener;
}
